<template>
  <div>
    <!-- dialog弹出框 -->
    <el-dialog
      title="评分"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <!-- from表单 -->
      <div class="from-infomations">
        <el-form ref="form" label-position="left" :model="sendInfo" label-width="8rem">
          <el-form-item label="姓名">
            {{ userInfo.userName }}
          </el-form-item>
          <el-form-item label="工号">
            {{ userInfo.userId }}
          </el-form-item>
          <el-form-item v-if="routerRules.routerScoreType ===1" label="评分机制">
            加分制
          </el-form-item>
          <el-form-item v-if="routerRules.routerScoreType ===2" label="评分机制">
            扣分制
          </el-form-item>
          <el-form-item v-if="routerRules.routerScoreType ===3" label="选择评分机制">
            <el-select v-model="scoreRules" style="width:13rem" placeholder="请选择" :change="getScoreType()">
              <el-option
                v-for="item in scoreAllRule"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="当前基础分">
            {{ routerRules.routerBaseScore }}分
          </el-form-item>
          <el-form-item label="可赋值最高分">
            {{ routerRules.routerMaxScore }}分
          </el-form-item>
          <el-form-item v-if="routerRules.routerMinScore !== null&&routerRules.routerMinScore !== -99999" label="可赋值最低分">
            {{ routerRules.routerMinScore }}分
          </el-form-item>
          <el-form-item v-if="routerRules.routerMinScore === -99999" label="可赋值最低分">
            该项目无最低分限制
          </el-form-item>
          <el-form-item v-if="sendScore === 2" label="当前用户分数">
            {{ sendInfo.sendUserScore }}分
          </el-form-item>
          <!-- 输入分数 -->
          <el-form-item v-if="scoreRules !== ''" label="分数">
            <el-tooltip class="form-score" effect="dark" :content="scoreTips" placement="bottom-start">
              <el-input
                v-model="sendInfo.sendScore"
                style="width:13rem"
                placeholder="请输入分数"
                clearable
                oninput="value=value.replace(/^\.+|[^\d.-]/g,'')"
                maxlength="4"
                show-word-limit
                @input="changeSendScore"
              />
            </el-tooltip>
          </el-form-item>
          <el-form-item v-if="scoreRules === ''" label="分数">
            请选择评分机制，选择后方可输入分数
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getAllInfo } from '../../api/user.js'
import { getSendInfo, getUpdateSendScore, getAddSend } from '../../api/send.js'
import { getSlist } from '../../api/slist.js'
import { getTlist } from '../../api/tlist.js'
import { isOpenSendTime } from '../../utils/isOpenSendTime.js'
export default {
  props: {
    // 状态获取
    getStatusType: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      // 获取全部数据存储位置
      show: false,
      sendInfo: {
        sendId: '',
        sendTitle: '',
        sendInfo: '',
        sendFlistId: '',
        sendSlistId: '',
        sendTlistId: '',
        sendUserId: '',
        sendStatus: '',
        sendScore: '',
        sendImages: '',
        sendGiveUserId: ''
      },
      userInfo: {
        userId: '',
        userPwd: '',
        userType: '',
        userBranch: '',
        userName: '',
        userReconsider: '',
        userScore: ''
      },
      routerRules: {
        routerId: '',
        routerName: '',
        routerScoreType: '',
        routerBaseScore: '',
        routerMaxScore: '',
        routerMinScore: ''
      },
      sendEndSendScore: '',
      // 代码数据存储位置
      sendStatus: '',
      scoreRules: '',
      scoreAllRule: [
        {
          value: '1',
          label: '加分制'
        },
        {
          value: '2',
          label: '扣分制'
        }
      ],
      scoreTips: '',
      sendScore: ''
    }
  },
  watch: {
  },
  inject: ['reload'],
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // 全部调用方法
    // 展示dialog
    showDialog(data) {
      this.show = true
      var that = this
      //  获取用户信息
      that.statusType = data
      new Promise(function(resolve, reject) {
        // 根据用户id获取用户信息
        var userId = that.statusType.userId
        getAllInfo(userId).then((res) => {
          resolve(res.obj)
        })
      }).then(data => {
        that.userInfo.userId = data.userId
        that.userInfo.userType = data.userType
        that.userInfo.userName = data.userinfo.userName
        that.userInfo.userBranch = data.userinfo.userBranch
        that.userInfo.userReconsider = data.userinfo.userReconsider
        that.userInfo.userScore = data.userinfo.userScore
        // 根据sendId判断send表
        var sendId = that.statusType.sendId
        if (sendId === null) {
          // 设置数据，保存当前状态
          that.sendStatus = 0
          // 获取当前路由分数类型和分数上下限
          that.findRouterRules()
        } else {
          // 设置数据，保存当前状态
          that.sendStatus = 1
          // send表中有数据，根据sendId查找send表中的信息
          that.findSend(sendId)
        }
      })
    },
    // 根据sendId查询send表中的信息
    findSend(sendId) {
      var that = this
      that.sendInfo.sendId = sendId
      new Promise(function(resolve, reject) {
        getSendInfo(sendId).then((res) => {
          console.log(res)
          resolve(res.obj)
        })
      }).then((data) => {
        if (that.routerRules.routerScoreType === 2 && data.sendScore !== null) {
          that.sendInfo.sendScore = that.routerRules.routerBaseScore - data.sendScore
        }
        // 存储最终数据，用户展示
        that.sendInfo.sendUserScore = parseFloat(data.sendScore)
        // 判断是否有分数，用于展示用户基础分数
        if (data.sendScore === null || that.sendInfo.sendScore === 'NaN') {
          that.sendScore = 1
        } else if (that.sendInfo.sendScore !== null) {
          that.sendScore = 2
        }
        // 获取当前路由分数类型和分数上下限
        that.findRouterRules()
      })
    },
    // 获取当前路由分数类型和分数上下限
    findRouterRules() {
      var that = this
      var slist = sessionStorage.getItem('slist')
      var tlist = sessionStorage.getItem('tlist')
      if (tlist === 'nolook') {
        new Promise(function(resolve, reject) {
          getSlist(slist).then(res => {
            resolve(res.obj)
          })
        }).then((data) => {
          that.routerRules.routerId = data.slistId
          that.routerRules.routerName = data.slistName
          that.routerRules.routerScoreType = data.slistScoreType
          that.routerRules.routerBaseScore = data.slistBaseScore
          that.routerRules.routerMaxScore = data.slistMaxScore
          that.routerRules.routerMinScore = data.slistMinScore
          if (data.slistScoreType !== 3) {
            that.scoreRules = data.slistScoreType
            if (data.slistScoreType === 1) {
              that.scoreTips = '当前为加分制，输入1即代表加1分'
            } else if (data.slistScoreType === 2) {
              that.scoreTips = '当前为扣分制，输入1即代表扣1分'
            }
          } else if (data.slistScoreType === 3) {
            that.scoreRules = ''
          }
        })
      } else {
        new Promise(function(resolve, reject) {
          getTlist(tlist).then(res => {
            resolve(res.obj)
          })
        }).then((data) => {
          that.routerRules.routerId = data.tlistId
          that.routerRules.routerName = data.tlistName
          that.routerRules.routerScoreType = data.tlistScoreType
          that.routerRules.routerBaseScore = data.tlistBaseScore
          that.routerRules.routerMaxScore = data.tlistMaxScore
          that.routerRules.routerMinScore = data.tlistMinScore
          if (data.tlistScoreType !== 3) {
            that.scoreRules = data.tlistScoreType
            if (data.tlistScoreType === 1) {
              that.scoreTips = '当前为加分制，输入1即代表加1分'
            } else if (data.tlistScoreType === 2) {
              that.scoreTips = '当前为扣分制，输入1即代表扣1分'
            }
          } else if (data.tlistScoreType === 3) {
            that.scoreRules = ''
          }
        })
      }
    },
    // 初次提交，添加信息
    addSend() {
      var that = this
      var send = {}
      send.sendScore = that.sendEndSendScore
      send.sendUserId = that.getStatusType.userId
      send.sendFlistId = sessionStorage.getItem('flist')
      send.sendSlistId = sessionStorage.getItem('slist')
      send.sendSemseterId = sessionStorage.getItem('semId')
      var tlist = sessionStorage.getItem('tlist')
      if (tlist !== 'nolook') {
        send.sendTlistId = tlist
      } else {
        send.sendTlistId = 'nolook'
      }
      send.sendGiveUserId = sessionStorage.getItem('userId')
      // 添加send表中的信息
      new Promise(function(resolve, reject) {
        getAddSend(send).then((res) => {
          resolve(res.result)
        })
      }).then((data) => {
        if (data === true) {
          that.show = false
          that.$message({
            message: '添加成功',
            type: 'success'
          })
          top.location.reload()
        }
      })
    },
    // 再次提交，修改信息
    updateSend() {
      var that = this
      var sendEndSendScore = that.sendEndSendScore
      that.sendInfo.sendGiveUserId = sessionStorage.getItem('userId')
      var sendInfo = that.sendInfo
      // 根据sendId修改信息
      new Promise(function(resolve, reject) {
        getUpdateSendScore(sendInfo, sendEndSendScore).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        if (data === null) {
          that.show = false
          that.$message({
            message: '修改成功',
            type: 'success'
          })
          top.location.reload()
        }
      })
    },

    // 页面逻辑方法
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },
    // 查看当前分数类型
    getScoreType() {
      var that = this
      var scoreRules = that.scoreRules
      if (scoreRules === '1') {
        that.scoreTips = '当前为加分制，输入1即代表加1分'
      } else if (scoreRules === '2') {
        that.scoreTips = '当前为扣分制，输入1即代表扣1分'
      }
    },
    // 获取当前分数
    changeSendScore(e) {
      var that = this
      that.sendInfo.sendScore = e
    },
    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.show = false
        this.sendInfo.sendScore = ''
        this.$message({
          type: 'success',
          message: '取消评分成功'
        })
      }).catch(() => {})
    },
    // 提交dialog
    sendDialog() {
      var that = this
      var sendInfo = that.sendInfo
      var routerMaxScore = that.routerRules.routerMaxScore
      var routerMinScore = that.routerRules.routerMinScore
      var sendEndScore = that.sendInfo.sendScore
      var scoreRules = that.scoreRules
      var sendEndSendScore = ''
      var sendUserScore = that.sendInfo.sendUserScore
      // 看下是否在开放时间内
      isOpenSendTime().then((res) => {
        that.timeType = res
        if (that.timeType === 1) {
          if (sendUserScore === undefined || sendUserScore === null || sendUserScore === '') {
            sendUserScore = parseFloat(that.routerRules.routerBaseScore)
            if (scoreRules === '2' || scoreRules === 2) {
              sendEndSendScore = -sendEndScore
            } else if (scoreRules === '1' || scoreRules === 1) {
              sendEndSendScore = sendEndScore
            }
          } else {
            if (scoreRules === '2' || scoreRules === 2) {
              sendEndSendScore = -sendEndScore
              sendUserScore = parseFloat(sendUserScore)
            } else if (scoreRules === '1' || scoreRules === 1) {
              sendEndSendScore = sendEndScore
              sendUserScore = parseFloat(sendUserScore)
            }
          }
          var giveSendEndSendScore = parseFloat(sendEndSendScore) + sendUserScore
          that.sendEndSendScore = giveSendEndSendScore
          // 全部逻辑判断
          if (that.scoreRules === '') {
            that.$message({
              message: '请选择评分机制',
              type: 'warning'
            })
          } else if (sendInfo.sendScore === '') {
            that.$message({
              message: '请输入分数',
              type: 'warning'
            })
          } else if (giveSendEndSendScore > routerMaxScore) {
            that.$message({
              message: '请不要大于最高分',
              type: 'warning'
            })
          } else if (giveSendEndSendScore < -30) {
            that.$message({
              message: '请不要扣除超过30分',
              type: 'warning'
            })
          } else if (giveSendEndSendScore > 50) {
            that.$message({
              message: '请不要增加超过50分',
              type: 'warning'
            })
          } else if (giveSendEndSendScore < routerMinScore && routerMinScore !== null) {
            that.$message({
              message: '请不要低于最低分',
              type: 'warning'
            })
          } else {
            // 逻辑判断通过，准备提交
            new Promise(function(resolve, reject) {
              that.$confirm('是否确定提交', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                that.$message('正在提交中')
                resolve(1)
              }).catch(() => {
                resolve(2)
              })
            }).then((data) => {
              if (data === 1) {
                if (that.getStatusType.sendId === null) {
                  // 初次提交，添加信息
                  that.addSend()
                } else if (that.getStatusType.sendId !== null) {
                  // 再次提交，修改信息
                  that.updateSend()
                } else {
                  that.$message({
                    message: '当前服务器繁忙，请稍后再试',
                    type: 'warning'
                  })
                }
              } else if (data === 2) {
                this.$message('请继续编辑')
              }
            })
          }
        } else if (that.timeType === 2) {
          that.$message({
            message: '未到开放时间',
            type: 'warning'
          })
        } else if (that.timeType === 3) {
          that.$message({
            message: '已过开放时间',
            type: 'warning'
          })
        } else {
          that.$message({
            message: '系统繁忙请联系管理员',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss">
.from-infomations{
    margin-left: 1rem;
    margin-right: 1.5rem;
}
.sendInfoImage{
    max-width:30rem;
    height: 10rem;
}
.form-score{
  margin: 4px;
}
</style>
